package org.pms.demo.webservice.utils;

import org.pms.demo.webservice.exception.IMReadException;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by laijianpeng on 17/10/16.
 */
public class DateUtil {
    /**
     * 获取上个月
     * @return
     */
    public static Date getLastMonth(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -1);
        return cal.getTime();
    }
    /**
     * 获取上个月最后一天
     * @return
     */
    public static int getLastMonthLastDay(){
        //获取当前时间
        Calendar cal = Calendar.getInstance();
		/*//下面可以设置月份，注：月份设置要减1，所以设置1月就是1-1，设置2月就是2-1，如此类推
		cal.set(Calendar.MONTH, 1-1);*/
        //调到上个月
        cal.add(Calendar.MONTH, -1);
        //得到一个月最最后一天日期(31/30/29/28)
        int maxDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        return maxDay;
    }
    /**
     * 月差
     * @param fDate
     * @param oDate
     * @return
     */
    public static int monthOfTwo(Date fDate, Date oDate) {
        int month1 = getMonthNum(fDate);
        int month2 = getMonthNum(oDate);
        return month2 - month1;

    }

    /**
     * 周差
     * @param fDate
     * @param oDate
     * @return
     */
    public static int weekOfTwo(Date fDate, Date oDate) {
        int week1 = getWeekNum(fDate);
        int week2 = getWeekNum(oDate);
        return week2 - week1;

    }

    /**
     * 天差
     * @param fDate
     * @param oDate
     * @return
     */
    public static int daysOfTwo(Date fDate, Date oDate) {

        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(fDate);
        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
        aCalendar.setTime(oDate);
        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
        return day2 - day1;

    }

    /**
     * 获取年
     * @param date
     * @return
     */
    public static int getYear(Date date){
        Calendar ca = Calendar.getInstance();//创建一个日期实例
        ca.setTime(date);//实例化一个日期
        return ca.get(Calendar.YEAR);
    }
    /**
     * 获取第几月
     * @param date
     * @return
     */
    public static int getMonthNum(Date date){
        Calendar ca = Calendar.getInstance();//创建一个日期实例
        ca.setTime(date);//实例化一个日期
        return ca.get(Calendar.MONTH)+1;//获取是第几月
    }
    public static int getDateNum(Date date){
        Calendar ca = Calendar.getInstance();//创建一个日期实例
        ca.setTime(date);//实例化一个日期
        return ca.get(Calendar.DATE);
    }
    /**
     * 根据时间戳获取第几周
     * @param date
     * @return
     */
    public static int getWeekNum(Date date){
        Calendar ca = Calendar.getInstance();//创建一个日期实例
        ca.setTime(date);//实例化一个日期
        return ca.get(Calendar.WEEK_OF_YEAR);//获取是第几周
    }
    public static int compare_date(String DATE1, String DATE2,String format) {
        DateFormat df = new SimpleDateFormat(format);
        try {
            Date dt1 = new Date();
            if(!StringUtils.isNullOrEmpty(DATE1)){
                dt1 = df.parse(DATE1);
            }
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                //System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                //	System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
    public static int compare_date(String DATE1, String DATE2) {
        String format= "yyyy-MM-dd HH:mm:ss";
        return compare_date(DATE1,DATE2,format);
    }


    /**
     * 日期转换成字符串
     * @Title: getString
     * @param date
     * @param unq   yyyy-MM-dd HH:mm:ss
     * @return
     * @return String
     * @throws
     */
    public static String getString(Date date, String unq){
        if(date==null){
            date = new Date();
        }
        SimpleDateFormat format = new SimpleDateFormat(unq);
        return format.format(date);
    }
    public static String getStringNotNowDate(Date date, String unq){
        if(date==null){
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat(unq);
        return format.format(date);
    }
    public static Date getDateByTime(Long time){
        return new Date(Long.valueOf(time));
    }
    public static String getDistanceTime(Long diff) {

        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        day = diff / (24 * 60 * 60 * 1000);
        hour = (diff / (60 * 60 * 1000) - day * 24);
        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        sec = (diff/1000-day*24*60*60-hour*60*60-min*60);

        return  hour + "时" + min + "分" + sec + "秒";
    }
    public static String getDistanceDate(Long diff) {

        long day = 0;
        long hour = 0;
        long min = 0;
        //	long sec = 0;
        day = diff / (24 * 60 * 60 * 1000);
        hour = (diff / (60 * 60 * 1000) - day * 24);
        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        //  sec = (diff/1000-day*24*60*60-hour*60*60-min*60);

        return  day+"天"+hour + "时" + min + "分";
    }
    public static String getDistanceDay(Long diff) {

        long day = 0;
        long hour = 0;
        long min = 0;
		/*long sec = 0; */
        day = diff / (24 * 60 * 60 * 1000);
        hour = (diff / (60 * 60 * 1000) - day * 24);
        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
       /* sec = (diff/1000-day*24*60*60-hour*60*60-min*60); */
        if(hour>=8L){
            hour=8L;
        }
        double minD =min/60.0+hour;

        return  minD+"";
    }
    /**
     * 字符串转换成日期
     * @Title: StrToDate
     * @param str
     * @param unq
     * @return
     * @return Date
     * @throws
     */
    public static Date StrToDate(String str, String unq) {
        SimpleDateFormat format = new SimpleDateFormat(unq);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            Map<String,Object> errorJson = new HashMap<String,Object>();
            errorJson.put("str", str);
            errorJson.put("unq", unq);
            IMReadException.printf(e, "类型转换异常！",errorJson);
        }
        return date;
    }


}
